Imports System
Imports System.IO
Imports System.String
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text.RegularExpressions
Imports IDServer


Public Class Frm_SALRetailPromotion
    Implements IDServer.IDServer.IDServer



    Public ActionHelp As IDServer.IDServer.ActionBtn = IDServer.IDServer.ActionBtn.ActionHelp
    Private mDataset As DataSet

#Region "interface define"


    Public Function ActionAdd() As Boolean Implements IDServer.IDServer.IDServer.ActionAdd
        ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd
        ClearText()
        SetEnable(True)
    End Function

    Public Function ActionCancel() As Boolean Implements IDServer.IDServer.IDServer.ActionCancel
        ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
        SetEnable(False)
    End Function

    Public Function ActionDelete() As Boolean Implements IDServer.IDServer.IDServer.ActionDelete
        Try
            If MsgBox(My.Resources.ConfirmDelete, MsgBoxStyle.Question + MsgBoxStyle.OkCancel, Msg.CompanyName) = MsgBoxResult.Ok Then
                If DeleteFromDataBase() = True Then
                    MsgBox(My.Resources.DeleteSuccess, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                    ClearText()
                    SetEnable(False)
                End If
            End If
        Catch ex As Exception
            MsgBox("ActionDelete:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Public Function ActionEdit() As Boolean Implements IDServer.IDServer.IDServer.ActionEdit
        ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit
        SetEnable(True)
    End Function

    Public Function ActionExport() As Boolean Implements IDServer.IDServer.IDServer.ActionExport

    End Function

    Public Function ActionFilter() As Boolean Implements IDServer.IDServer.IDServer.ActionFilter

    End Function

    Public Function ActionGoFirst() As Boolean Implements IDServer.IDServer.IDServer.ActionGoFirst

    End Function

    Public Function ActionGoLast() As Boolean Implements IDServer.IDServer.IDServer.ActionGoLast

    End Function

    Public Function ActionGoNext() As Boolean Implements IDServer.IDServer.IDServer.ActionGoNext

    End Function

    Public Function ActionGoPrevious() As Boolean Implements IDServer.IDServer.IDServer.ActionGoPrevious

    End Function

    Public Function ActionImport() As Boolean Implements IDServer.IDServer.IDServer.ActionImport

    End Function

    Public Function ActionInitialize() As Boolean Implements IDServer.IDServer.IDServer.ActionInitialize

    End Function

    Public Function ActionPrint() As Boolean Implements IDServer.IDServer.IDServer.ActionPrint

    End Function

    Public Function ActionSave() As Boolean Implements IDServer.IDServer.IDServer.ActionSave
        If BtnActionSave() = True Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function ActionSelect() As Boolean Implements IDServer.IDServer.IDServer.ActionSelect

    End Function

#End Region


#Region "sub or function"


    Private Sub SetEnable(ByVal B As Boolean)
        Me.txtRate2.Enabled = B
        Me.txtStartDate.Enabled = B
        Me.txtEndDate.Enabled = B
        Me.txtStartDate1.Enabled = B
        Me.txtEndDate1.Enabled = B
        Me.txt1.Enabled = B
        Me.txt2.Enabled = B
        Me.txt3.Enabled = B
        Me.txt4.Enabled = B
        Me.RadioButton1.Enabled = B
        Me.RadioButton2.Enabled = B
        Me.ListView1.Enabled = B
        Me.ListView2.Enabled = B
    End Sub

    Private Sub ClearText()
        Me.txtRate2.Text = ""
        Me.txtStartDate.Text = ""
        Me.txtEndDate.Text = ""
        Me.txtStartDate1.Text = ""
        Me.txtEndDate1.Text = ""
        Me.txt1.Text = ""
        Me.txt2.Text = ""
        Me.txt3.Text = ""
        Me.txt4.Text = ""
    End Sub

    Private Sub LoadGoodsName()
        Try

            Dim sql As String = ""
            Dim mds As New DataSet
            Dim Item As ListViewItem
            Dim mColor As Boolean
            sql = "select * from BSC_Goods"
            mds = ReturnDataset(sql)
            If mds.Tables(0).Rows.Count > 0 Then
                For Each mRow As DataRow In mds.Tables(0).Rows
                    Item = New ListViewItem
                    Item.Text = mRow("CnName") & ""
                    Item.Tag = mRow("code") & ""
                    If mColor = True Then
                        Item.BackColor = Color.AliceBlue
                    End If
                    mColor = Not mColor
                    Me.ListView1.Items.Add(Item)
                    'Me.ListView2.Items.Add(Item)
                Next
            End If
        Catch ex As Exception
            MsgBox("LoadGoodsName:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Function BtnActionSave() As Boolean
        Try
            Dim sql As String = ""
            Dim fRate As String = ""
            Dim fRate1 As String = Me.txtRate2.Text.Trim
            Dim fStartDate As String = Me.txtStartDate.Text.Trim
            Dim fEndDate As String = Me.txtEndDate.Text.Trim
            Dim fStartDateA As String = Me.txtStartDate1.Text.Trim
            Dim fEndDateA As String = Me.txtEndDate1.Text.Trim
            Dim fGoodsCodeA As String = ""
            Dim fGoodsCodeB As String = ""
            Dim fRadioTag As String = 0
            Dim fCountA As String = 0
            Dim fCountB As String = 0

            'If ActionInitialize() = False Then Exit Function

            If Me.RadioButton1.Checked = True Then
                fRadioTag = 0
                fCountA = Me.txt1.Text.Trim
                fCountB = Me.txt2.Text.Trim
            End If
            If Me.RadioButton2.Checked = True Then
                fRadioTag = 1
                fCountA = Me.txt3.Text.Trim
                fCountB = Me.txt4.Text.Trim
            End If

            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Then
                sql = "Insert Into BSC_SalesPromotion(fMemberRate,fGoodsRate2,fGoodsCodeA,fStartDate,fEndDate,fRadioTag,fCountA,fCountB,fStartDateA,fEndDateA,fGoodsCodeB) values(" & _
                      fRate & "," & fRate1 & ",'" & fGoodsCodeA & "','" & fStartDate & "','" & fEndDate & "'," & fRadioTag & "," & fCountA & "," & fCountB & ",'" & fStartDateA & "','" & fEndDateA & "','" & fGoodsCodeB & "')"
                ReturnObject(sql)
                For i As Integer = 0 To Me.ListView1.CheckedItems.Count - 1
                    sql = "Update BSC_Goods set SaleStatus=1 where Code='" & Me.ListView1.CheckedItems(i).Tag.ToString & "'"
                    ReturnObject(sql)
                Next
            ElseIf ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then
                sql = "Update BSC_SalesPromotion Set fMemberRate=" & fRate & ",fGoodsRate2=" & fRate1 & ",fGoodsCodeA='" & fGoodsCodeA & "',fStartDate='" & fStartDate & "',fEndDate='" & fEndDate & _
                      "',fRadioTag=" & fRadioTag & ",fCountA=" & fCountA & ",fCountB=" & fCountB & ",fStartDateA='" & fStartDateA & "',fEndDateA='" & fEndDateA & "',fGoodsCodeB='" & fGoodsCodeB & "' where ID=" & Me.LabID.Text
                ReturnObject(sql)
                For i As Integer = 0 To Me.ListView1.CheckedItems.Count - 1
                    sql = "Update BSC_Goods set SaleStatus=1 where Code='" & Me.ListView1.CheckedItems(i).Tag.ToString & "'"
                    ReturnObject(sql)
                Next
            End If
            MsgBox(IIf(ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd, My.resources.Addsuccess, My.resources.EditSuccess), MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
            ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
            SetEnable(False)
            Return True
        Catch ex As Exception
            MsgBox("BtnActionSave:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Private Function DeleteFromDataBase() As Boolean
        Try

            Dim sql As String = ""
            sql = "Delete BSC_SalesPromotion where Id=" & Me.LabID.Text
            ReturnObject(sql)
            Return True
        Catch ex As Exception
            MsgBox("DeleteFromDataBase:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Private Sub FillText()
        Try

            Dim sql As String = ""
            Dim fGoodsCodeA As String = ""
            Dim fGoodsCodeB As String = ""
            sql = "select * from BSC_SalesPromotion"
            mDataset = New DataSet
            mDataset = ReturnDataset(sql)
            If mDataset.Tables(0).Rows.Count > 0 Then
                For Each mRow As DataRow In mDataset.Tables(0).Rows
                    Me.LabID.Text = mRow("ID") & ""
                    Me.txtRate2.Text = mRow("fGoodsRate2") & ""
                    Me.txtStartDate.Value = CType(mRow("fStartDate") & "", Date)
                    Me.txtEndDate.Value = CType(mRow("fEndDate") & "", Date)
                    If mRow("fRadioTag") & "" = 0 Then
                        Me.RadioButton1.Checked = True
                        Me.txt1.Text = mRow("fCountA") & ""
                        Me.txt2.Text = mRow("fCountB") & ""
                    Else
                        Me.RadioButton2.Checked = True
                        Me.txt3.Text = mRow("fCountA") & ""
                        Me.txt4.Text = mRow("fCountB") & ""
                    End If
                    Me.txtStartDate1.Value = CType(mRow("fStartDateA") & "", Date)
                    Me.txtEndDate1.Value = CType(mRow("fEndDateA") & "", Date)
                    fGoodsCodeA = mRow("fGoodsCodeA") & ""
                    fGoodsCodeB = mRow("fGoodsCodeB") & ""
                Next
                For i As Integer = 0 To fGoodsCodeA.Split("^").Length - 1
                    For j As Integer = 0 To Me.ListView1.Items.Count - 1
                        If Me.ListView1.Items(j).Tag.ToString = fGoodsCodeA.Split("^")(i).ToString Then
                            Me.ListView1.Items(j).Checked = True
                        End If
                    Next
                Next
            Else
                Exit Sub
            End If
        Catch ex As Exception
            MsgBox("FillText:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub
#End Region

    Private Sub Frm_SALRetailPromotion_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            LoadGoodsName()
            SetEnable(False)
            FillText()
        Catch ex As Exception
            MsgBox("Frm_SALRetailPromotion_Load:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub txtRate_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        Try
            'Dim Input As String = Me.txtRate.Text.Substring(0, Me.txtRate.SelectionStart) + e.KeyChar + Me.txtRate.Text.Substring(Me.txtRate.SelectionStart, Me.txtRate.SelectionLength)
            'e.Handled = Not IsMatch(Input, 0)
        Catch ex As Exception
            MsgBox("txtRate_KeyPress:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub


    Private Sub txtRate2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        Try
            Dim Input As String = Me.txtRate2.Text.Substring(0, Me.txtRate2.SelectionStart) + e.KeyChar + Me.txtRate2.Text.Substring(Me.txtRate2.SelectionStart, Me.txtRate2.SelectionLength)
            e.Handled = Not IsMatch(Input, 0)
        Catch ex As Exception
            MsgBox("txtRate_KeyPress:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub txt1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        Try
            If Not IsNumeric(e.KeyChar) Then
                e.Handled = True
            Else
                e.Handled = False
            End If
        Catch ex As Exception
            MsgBox("txt1_KeyPress:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub txtMoney_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.txtMoney.TextAlign = HorizontalAlignment.Left
    End Sub

    Private Sub txtMoney_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If Char.IsControl(e.KeyChar) = True Then Exit Sub
        If Not IsNumeric(e.KeyChar) Then
            e.Handled = True
        Else
            e.Handled = False
        End If
    End Sub

    Private Sub txtMoney_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs)
        If Me.txtMoney.Text.Length > 0 Then
            Me.txtMoney.TextAlign = HorizontalAlignment.Right
        End If
    End Sub

    Private Sub txtGiftCode_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtGiftCode.KeyDown, txtGiftName.KeyDown, StartDate.KeyDown, EndDate.KeyDown, txtMoney.KeyDown, txtCount.KeyDown, txtStartNum.KeyDown, txtEndNum.KeyDown
        Try

            If e.KeyCode = Keys.Shift Then
                e.Handled = True
                SendKeys.Send("+{TAB}")
                SendKeys.Send("{F4}")
            Else
                Select Case e.KeyCode
                    Case Keys.Enter
                        SendKeys.Send("{TAB}")
                        SendKeys.Send("{F4}")
                End Select
            End If
        Catch ex As Exception
            MsgBox("txtGiftCode_KeyDown:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub BtnBarCode_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnBarCode.Click
        Try
            If Control.ModifierKeys = Keys.Shift Then
                SendKeys.Send("+{TAB}")
            End If
            Dim Num As Integer = Me.txtCount.Text
            Dim Item As String = ""
            Dim Code As String = Me.txtGiftCode.Text
            Dim mNum As String = PubCompanyCode & Code
            Dim nNum As String = ""
            Me.ListBox1.Items.Clear()

            For i As Integer = 0 To Num - 1
                nNum = Format(i, "00000")
                Me.ListBox1.Items.Add(mNum & nNum)
            Next
        Catch ex As Exception
            MsgBox("BtnBarCode_Click:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub
End Class
